package com.base.api;

import com.alibaba.fastjson.JSONObject;
import com.base.core.aop.annotation.ApiLog;
import com.base.core.model.ApiRes;
import com.base.core.model.EmailData;
import com.base.service.MailService;
import com.base.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 示例Controller
 * @author 177****0334
 * @date 2020/12/23 09:49
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户UserController")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private MailService mailService;

    /**
     * 创建 微信登录
     * @author 177****0334
     * @date 2020/12/23 09:49
     */
    @PostMapping("/login/wx")
    @ApiOperation("创建")
    public ApiRes create(@RequestBody JSONObject param) {
        return ApiRes.success(userService.login(param));
    }

    /**
     * 绑定手机号
     * @author 177****0334
     * @date 2020/12/23 09:49
     */
    @ApiLog
    @PostMapping("/bindmobile")
    @ApiOperation("绑定手机号")
    public ApiRes bindMobile(@RequestBody @ApiParam(name = "用户信息") JSONObject param) {
        return ApiRes.success(userService.bindMobile(param));
    }

    /**
     * 绑定手机号
     * @author 177****0334
     * @date 2020/12/23 09:49
     */
    @SneakyThrows
    @GetMapping("/sendEmail")
    @ApiOperation("绑定手机号")
    public ApiRes sendEmail() throws MessagingException {
        Map<String, Object> data = new HashMap<>(16);
        data.put("username", "测试模板");
        data.put("href", "https://cloud.tencent.com/developer/article/1532260#:~:text=Springboot%20mail%20%E6%96%87%E6%9C%AC%E9%82%AE%E4%BB%B6%20%E6%96%87%E6%9C%AC%E9%82%AE%E4%BB%B6%E6%98%AF%E6%9C%80%E7%AE%80%E5%8D%95%E4%B9%9F%E6%98%AF%E6%9C%80%E5%9F%BA%E7%A1%80%E7%9A%84%E4%B8%80%E7%A7%8D%E9%82%AE%E4%BB%B6%EF%BC%8C%E4%BD%BF%E7%94%A8%20Spring%20%E5%B0%81%E8%A3%85%E7%9A%84%20JavaMailSender%20%E7%9B%B4%E6%8E%A5%E5%8F%91%E9%80%81%E5%B0%B1%E5%8F%AF%E4%BB%A5%E4%BA%86%E3%80%82,MailService%20%E7%B1%BB%EF%BC%8C%E6%B3%A8%E5%85%A5%20JavaMailSender%20%E7%94%A8%E4%BA%8E%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%EF%BC%8C%E4%BD%BF%E7%94%A8%20%40Value%20%28%22%24%20%7Bspring.mail.username%7D%22%29%20%E7%BB%91%E5%AE%9A%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E5%8F%82%E6%95%B0%E7%94%A8%E4%BA%8E%E8%AE%BE%E7%BD%AE%E9%82%AE%E4%BB%B6%E5%8F%91%E9%80%81%E7%9A%84%E6%9D%A5%E9%82%AE%E7%AE%B1%E3%80%82");
        mailService.sendTemplate(
                new EmailData.Builder().setTo("17757140334@139.com")
                .setSubject("测试第一次发送")
                .build(), data, "RegisterSuccess").doIt();
        return ApiRes.success();
    }
}